<?php require 'inc_security.php'; 

//Kiem tra quyen addedit
checkPermission('add');

$adm_birthday = convertDateTime(getValue('adm_birthday','str','POST',''));
$myform = new generate_form();
$myform->addTable($bg_table);
$myform->add('adm_loginname','adm_loginname',0,0,'',1,'Tên đăng nhập không được bỏ trống',1,'Tên tài khoản đã được đăng ký');
$myform->add('adm_birthday','adm_birthday',1,1,0);
$myform->add('adm_mail','adm_mail',0,0,'');
$myform->add('adm_name','adm_name',0,0,'');
$myform->add('adm_phone','adm_phone',0,0,'');
$myform->add('adm_active','adm_active',1,0,0);
$action = getValue('action','str','POST','');
if($action == 'execute'){
	$password = getValue('adm_password','str','POST','');
	$repassword = getValue('repassword','str','POST','');
	if($password !== $repassword){
		$bg_errorMsg .= 'Mật khẩu nhập lại không đúng';
	}else{
		$adm_password = md5($password);
		$myform->add('adm_password','adm_password',0,1,'');	
	}
	$bg_errorMsg .= $myform->checkData();
	if(!$bg_errorMsg){
		$db_ex = new db_execute_return();
		$last_id = $db_ex->db_execute($myform->generate_insert_SQL());
		unset($db_ex);
		//insert admin_users_right
		$record_module = getValue('module_id','arr','POST','');
		if($record_module){
			$count = count($record_module);
			for($i = 0; $i<$count; $i++){
				$module_id = $record_module[$i];
				$myform = new generate_form();
				$myform->add('adu_admin_id','last_id',1,1,0);
				$myform->add('adu_admin_module_id','module_id',1,1,0);
				$myform->add('adu_admin_edit','adu_edit'.$record_module[$i], 1, 0, 0);
				$myform->add('adu_admin_add','adu_add'.$record_module[$i], 1, 0, 0);
				$myform->add('adu_admin_delete','adu_delete'.$record_module[$i], 1, 0, 0);
				$myform->addTable('admin_users_right');
				$db_insert = new db_execute($myform->generate_insert_SQL());
			}
		}
		
		redirect('listing.html');
	}
}
$db_getallmodule = new db_query('SELECT * 
								 FROM modules
								 ORDER BY mod_order DESC');
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="vi" lang="vi" xmlns:og="http://ogp.me/ns#" xmlns:fb="https://www.facebook.com/2008/fbml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?=$load_header?>
<style>
	.list-role label {
		margin-bottom: 0;
		margin-top : 6px;
		margin-right : 20px;
	}
	.list-role input[type="checkbox"]{
		margin-top:-1px;
		margin-right:2px;
	}
</style>
</head>
<body>
<div class="module_header bold fix"><?=$module_name?></div>
<div id="wrapper">
    <?=$bg_errorMsg?>
	<?php $form = new form();
	?>
	<?=$form->form_open()?>
    <?=$form->textnote('Các trường có dấu (<span class="form-asterick">*</span>) là bắt buộc nhập')?>
	<?=$form->text(array('label'=>'Tên đăng nhập','name'=>'adm_loginname','id'=>'adm_loginname','require'=>1,'errorMsg'=>'Bạn chưa nhập tên đăng nhập'))?>
	<?=$form->password(array('label'=>'Mật khẩu','name'=>'adm_password','id'=>'adm_password','require'=>1,'errorMsg'=>'Bạn chưa nhập mật khẩu'))?>
	<?=$form->password(array('label'=>'Nhập lại mật khẩu','name'=>'repassword','id'=>'repassword','require'=>1, 'errorMsg'=>'Bạn chưa nhập lại mật khẩu'))?>
	<?=$form->text(array('label'=>'Email','name'=>'adm_mail','id'=>'adm_mail'))?>
	<?=$form->text(array('label'=>'Họ tên','name'=>'adm_name','id'=>'adm_name'))?>
	<?=$form->text(array('label'=>'Điện thoại','name'=>'adm_phone','id'=>'adm_phone'))?>
	<?=$form->text(array('label'=>'Ngày sinh','name'=>'adm_birthday','id'=>'adm_birthday','isdatepicker'=>1))?>
	<?=$form->checkbox(array('label'=>'Kích hoạt','name'=>'adm_active','id'=>'adm_active','value'=>1,'currentValue'=>1))?>
	<?//Phan quyen quan tri?>
	<label class="label-section">Phân quyền module</label>
	<? 
	while ($row=mysql_fetch_array($db_getallmodule->result)){
        if(file_exists("../../modules/" . $row["mod_path"] . "/inc_security.php")===false){
            if(file_exists("../../core/" . $row["mod_path"] . "/inc_security.php")===false){
                continue;
            }
        }
	?>
        <?=$form->form_group_custom($row['mod_name']);?>
            <div class="list-role">
                <label class="fl"><input type="checkbox" name="module_id[]" value="<?=$row['mod_id']?>">Truy cập</label>
                <label class="fl"><input type="checkbox" name="adu_add<?=$row['mod_id']?>" value="1">Thêm</label>
                <label class="fl"><input type="checkbox" name="adu_edit<?=$row['mod_id']?>" value="1">Sửa</label>
                <label class="fl"><input type="checkbox" name="adu_delete<?=$row['mod_id']?>" value="1">Xóa</label>
            </div>
	    <?=$form->form_group_custom('close')?>
	<?}?>
    <?=$form->form_action(array('label'=>array('Thêm mới','Nhập lại'),'type'=>array('submit','reset')))?>
	<?=$form->form_close()?>
</div>
</body>
</html>